home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK1.toast / Development Kits (Disc 1) / Apple Guide / Engineering / APISample / APISampleCW / Source / TDocument.cp < prev    next >
Encoding:
Text File  |  1994-12-02  |  6.8 KB  |  234 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    Apple Macintosh Developer Technical Support
  4. #
  5. #    MultiFinder-Aware Simple Application Framework
  6. #
  7. #    TDocument.cp
  8. #
  9. #    TDocument.cp    -    C++ source containing the implementations for a basic
  10. #                        document class as well as a implementation to maintain
  11. #                        a linked list of active documents.
  12. #
  13. #    Copyright © 1991 Apple Computer, Inc.
  14. #    All rights reserved.
  15. #
  16. #    Versions:    
  17. #            1.20                    10/91
  18. #            1.10                     07/89
  19. #            1.00                     04/89
  20. #
  21. #    Components:
  22. #            TDocument.h                July 9, 1989
  23. #            TDocument.cp            July 9, 1989
  24. #
  25. #
  26. ------------------------------------------------------------------------------*/
  27.  
  28. /*
  29. Segmentation strategy:
  30.  
  31.     This program has only one segment, since the issues
  32.     surrounding segmentation within a class's methods have
  33.     not been investigated yet.
  34.     
  35. SetPort strategy:
  36.  
  37.     Toolbox routines do not change the current port. In
  38.     spite of this, in this program we use a strategy of
  39.     calling SetPort whenever we want to draw or make calls
  40.     which depend on the current port. This makes us less
  41.     vulnerable to bugs in other software which might alter
  42.     the current port (such as the bug (feature?) in many
  43.     desk accessories which change the port on OpenDeskAcc).
  44.     Hopefully, this also makes the routines from this
  45.     program more self-contained, since they don't depend on
  46.     the current port setting. 
  47.  
  48. Clipboard strategy:
  49.  
  50.     This program does not maintain a private scrap.
  51.     Whenever a cut, copy, or paste occurs, we import/export
  52.     from the public scrap to TextEdit's scrap right away,
  53.     using the TEToScrap and TEFromScrap routines. If we did
  54.     use a private scrap, the import/export would be in the
  55.     activate/deactivate event and suspend/resume event
  56.     routines. 
  57. */
  58.  
  59. // Mac Includes
  60. #include <Types.h>
  61. #include <QuickDraw.h>
  62. #include <Fonts.h>
  63. #include <Events.h>
  64. #include <Controls.h>
  65. #include <Windows.h>
  66. #include <Menus.h>
  67. #include <TextEdit.h>
  68. #include <Dialogs.h>
  69. #include <Desk.h>
  70. #include <Scrap.h>
  71. #include <ToolUtils.h>
  72. #include <Memory.h>
  73. #include <SegLoad.h>
  74. #include <Files.h>
  75. #include <OSUtils.h>
  76. #include <Traps.h>
  77.  
  78. #include "TDocument.h"        // use the local version. If you make changes
  79.                             // that you've debugged and want other files
  80.                             // to use, simple copy this header file into
  81.                             // the C++ Includes folder (don't forget to
  82.                             // copy the TDocument object library to the C++
  83.                             // Libraries folder)!
  84. #ifndef __UAPPSHARED__
  85.     #include "UAppShared.h"
  86. #endif
  87.  
  88.  
  89. /***********************************************************************/
  90. //
  91. // TDocument class declarations
  92. //
  93. /***********************************************************************/
  94.  
  95. //-----------------------------------------------------------------------
  96. // TDocument::TDocument -     automatically open a new window for the document
  97. //                            using resID as the resource id for the window.
  98. //                            If you modify this so that it doesn't create a
  99. //                            window, be sure to take that into account in the
  100. //                            application class so that it doesn't believe an
  101. //                            error occurred (as version 1.20 of TApplication
  102. //                            would).
  103. //
  104.     TDocument::TDocument( short resID )
  105.     {
  106.         fDocWindow = GetNewWindow( resID, nil, (WindowPtr) -1 );
  107.         SetPort( fDocWindow );
  108.         
  109.     } /* TDocument (constructor) */
  110.  
  111.  
  112. //-----------------------------------------------------------------------
  113. // TDocument::~TDocument -    we need to deallocate the storage that was used
  114. //                            to create the document's window before we lose
  115. //                            access to it.
  116. //
  117.     TDocument::~TDocument( void )
  118.     {
  119.         DisposeWindow( fDocWindow );
  120.         
  121.     } /* TDocument (destructor) */
  122.  
  123.  
  124.  
  125. /***********************************************************************/
  126. //
  127. // TDocumentLink class declarations
  128. //
  129. /***********************************************************************/
  130.  
  131. //-----------------------------------------------------------------------
  132. // TDocumentLink::TDocumentLink -    simply initialize the linked list
  133. //                                    using the passed parameters.
  134. //
  135.     TDocumentLink::TDocumentLink( TDocumentLink *n, TDocument *v )
  136.     {
  137.         fNext = n;
  138.         fDoc = v;
  139.         
  140.     } /* TDocumentLink (constructor) */
  141.  
  142.  
  143.  
  144. /***********************************************************************/
  145. //
  146. // TDocumentList class declarations
  147. //
  148. /***********************************************************************/
  149.  
  150. //-----------------------------------------------------------------------
  151. // TDocumentList::TDocumentList -    since this is a brand new list,
  152. //                                    initialize the list to indicate that
  153. //                                    there are no active documents.
  154. //
  155.     TDocumentList::TDocumentList( void )
  156.     {
  157.         fDocList = nil;
  158.         fNumDocs = 0;
  159.         
  160.     } /* TDocumentList (constructor) */
  161.  
  162.  
  163. //-----------------------------------------------------------------------
  164. // TDocumentList::FindDoc -    given a pointer to a window, determine if the
  165. //                            window belongs to any document in the current
  166. //                            list of active documents. If it does, return
  167. //                            a pointer to that document; otherwise, return
  168. //                            nil.
  169. //
  170.     TDocument* TDocumentList::FindDoc( WindowPtr window )
  171.     {
  172.         TDocumentLink* temp;
  173.         TDocument* tDoc;
  174.     
  175.         for ( temp = fDocList; temp != nil; temp = temp->GetNext())
  176.         {
  177.             tDoc = temp->GetDoc();
  178.             if ( tDoc->GetDocWindow() == window )            // does the window belongs to the current document?
  179.               return tDoc;                                    // yes, let the calling routine know
  180.         }
  181.         return nil;                                            // sorry, none of the documents own that window
  182.         
  183.     } /* TDocumentList::FindDoc */
  184.  
  185.  
  186. //-----------------------------------------------------------------------
  187. // TDocumentList::AddDoc -    add the designated document to the linked list
  188. //                            of active documents. Also, be sure to update
  189. //                            the number of those active documents.
  190. //
  191.     void TDocumentList::AddDoc( TDocument* doc )
  192.     {
  193.         TDocumentLink* temp;
  194.     
  195.         temp = new TDocumentLink( fDocList, doc );
  196.         fDocList = temp;
  197.         fNumDocs++;
  198.         
  199.     } /* TDocumentList::AddDoc */
  200.  
  201.  
  202. //-----------------------------------------------------------------------
  203. // TDocumentList::RemoveDoc -    remove the designated document from the
  204. //                                linked list of active documents. Also, be
  205. //                                sure to update the number of those active
  206. //                                documents.
  207. //
  208.     void TDocumentList::RemoveDoc( TDocument* doc )
  209.     {
  210.         TDocumentLink* temp;
  211.         TDocumentLink* last;
  212.     
  213.         last = nil;
  214.         for ( temp = fDocList; temp != nil; temp = temp->GetNext())
  215.             if ( temp->GetDoc() == doc )                    // is the current document the one to be removed?
  216.             {                                                // yes, remove it
  217.                 // if first item in list, just set first
  218.                     if ( last == nil )                
  219.                         fDocList = temp->GetNext();
  220.                     else
  221.                         last->SetNext( temp->GetNext());
  222.                 
  223.                 // free the TDocumentLink        
  224.                     delete temp;                    
  225.                     fNumDocs--;
  226.                     
  227.                 // we're done; no need to continue onto the next document
  228.                     return;
  229.             }
  230.             else                                            // no, go onto the next document in the list
  231.                 last = temp;
  232.                 
  233.     } /* TDocumentList::RemoveDoc */
  234.